package com.spark.rectify

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.spark.colliery.common.ext.gone
import com.spark.common.BaseFragment
import com.spark.common.helper.MutableStatePagerAdapter
import kotlinx.android.synthetic.main.frag_handle.*

class RectMessageFragment : BaseFragment() {
    private lateinit var rectMessageAdapter: MutableStatePagerAdapter

    companion object {
        fun newInstance(): RectMessageFragment {
            val target = RectMessageFragment()
            val args = Bundle()

            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_handle
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        /**
         * 处罚通知
         *      纠违处罚单 PunishOrderPreviewFragment
         * 纠违超时
         *      通知内容
         *      处罚单 PunishOrderPreviewFragment
         * 销号通知
         *      通知内容
         *      处罚单 PunishOrderPreviewFragment

         */
        val state = 1
        val tabTitles = mutableListOf<String>()
        val frags = mutableListOf<Fragment>()
        when (state) {
            // 处罚通知
            0 -> {
                tabTitles.add("处罚单")
                frags.add(PunishOrderPreviewFragment.newInstance())
            }
            // 纠违超时
            // 销号通知
            1, 2 -> {
                tabTitles.apply {
                    add("通知内容")
                    add("处罚单")
                }
                frags.apply {
                    add(RectMessageNotifyFragment.newInstance())
                    add(PunishOrderPreviewFragment.newInstance())
                }
            }
        }
        rectMessageAdapter = object : MutableStatePagerAdapter(childFragmentManager) {
            override fun getCount(): Int {
                return tabTitles.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return tabTitles[position]
            }
        }
        tabs.gone(tabTitles.size == 1)
        tabTitles.forEach {
            tabs.addTab(tabs.newTab().setText(it))
        }
        pager.apply {
            adapter = rectMessageAdapter
            canScroll = true
        }
        tabs.setupWithViewPager(pager)
    }
}